package cn.tj212.xiaoyin.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private final SimpleDateFormat format;

    public DateUtil(SimpleDateFormat format) {
        this.format = format;
    }

    public SimpleDateFormat getFormat() {
        return format;
    }

    //紧凑型日期格式
    public static final DateUtil COMPAT=new DateUtil(new SimpleDateFormat("yyyyMMdd"));

    //紧凑型日期格式全格式
    public static final DateUtil COMPAT_FULL=new DateUtil(new SimpleDateFormat("yyyyMMddHHmmss"));

    //常用日期类型
    public static final DateUtil COMMON=new DateUtil(new SimpleDateFormat("yyyy-MM-dd"));
    public static final DateUtil COMMON_FULL=new DateUtil(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    //用斜线分隔的，多为西方采用的格式
    public static final DateUtil SLASH=new DateUtil(new SimpleDateFormat("yyyy/MM/dd"));

    //中文日期常用格式：yyyy年MM月dd日
    public static final DateUtil CHINESE=new DateUtil(new SimpleDateFormat("yyyy年MM月dd日"));
    public static final DateUtil CHINESE_FULL=new DateUtil(new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"));

    /**
     * 获取日期字符串
     * @param date
     * @return
     */
    public String getDateText(Date date){
        return getFormat().format(date);
    }

    /**
     * 字符串转日期
     * @param text
     * @return
     */
    public Date getTextDate(String text) throws ParseException {
        return getFormat().parse(text);
    }

    /**
     * 日期按指定模板转为字符串
     * @param date
     * @param format
     * @return
     */
    public static String getDateText(Date date,String format){
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 字符串按指定模板转为日期
     * @param dateText
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date getTextDate(String dateText,String format) throws ParseException {
        return new SimpleDateFormat(format).parse(dateText);
    }

    /**
     * 根据传入的日期获取星期几
     * @param date
     * @return
     */
    public static int getWeekDay(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int w=calendar.get(Calendar.DAY_OF_WEEK);
        int ret;
        if (w==Calendar.SUNDAY)
            ret=7;
        else
            ret=w-1;
        return ret;
    }

    public static int getAge(Date birthday){
        Calendar calendar=Calendar.getInstance();
        if (calendar.before(birthday)){
            throw new IllegalArgumentException("birthday later than present date!");
        }
        int yearNow=calendar.get(Calendar.YEAR);
        int monthNow=calendar.get(Calendar.MONTH)+1;//月份从0开始，此处必须要+1，否则结果是错误的
        int dayOfMonthNow=calendar.get(Calendar.DAY_OF_MONTH);
        calendar.setTime(birthday);
        int yearBirth=calendar.get(Calendar.YEAR);
        int monthBirth=calendar.get(Calendar.MONTH);
        int dayOfMonthBirth=calendar.get(Calendar.DAY_OF_MONTH);
        int age=yearNow-yearBirth;
        if (monthNow<monthBirth||(monthNow==monthBirth&&dayOfMonthNow<dayOfMonthBirth))
            age--;
        return age;
    }
}
